import axios, { AxiosInstance } from 'axios'
import appEnvironment from '@/app-environments'
import { getToken  } from '@/utils'

const {
    networkHosts: {
        apiServer1: {
            fullHost,
        },
    },
} = appEnvironment

const http: AxiosInstance = axios.create({
    baseURL: fullHost,
    timeout: 2 * 60 * 1000, // 2 分钟
})

http.interceptors.request.use((config) => {

    const token = getToken()
    if(token) {
        config.headers['Authorization'] = 'Bearer ' + token
    }
    return config
})

http.interceptors.response.use((res) => {

    const {
        data, status, 
        data: { message }
    } = res

    if(status===200) {
        return data
    } else {
        ElMessage({
            type: 'error',
            message: `${message} || '服务器异常'`,
        })
    }
})

export default http
